<%@page import="org.springframework.security.core.context.SecurityContextHolder"%>
<%@page import="com.labot.types.AuditType"%>
<%@page import="com.labot.types.AuditEventType"%>
<%@page import="com.labot.webapp.listener.StartupListener"%>
<%@ include file="/common/taglibs.jsp"%>
<%@ page import="org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices" %>
<%@ page import="javax.servlet.http.Cookie" %>

<%
	String username = "unknown";
try {
	username = SecurityContextHolder.getContext().getAuthentication().getName().toString();
} catch (Exception e) { }
Boolean invalidate = false;
if (request.getSession(false) != null) {
	
	if (request.getSession(false).getAttribute("invalidate") != null) {
		invalidate = true;
	}
	
    session.invalidate();
}
Cookie terminate = new Cookie(TokenBasedRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY, null);
String contextPath = request.getContextPath();
terminate.setPath(contextPath != null && contextPath.length() > 0 ? contextPath : "/");
terminate.setMaxAge(0);
response.addCookie(terminate);

StartupListener.getAuditManager().log(AuditEventType.USER_LOGGED_OUT, AuditType.INFO, "logout.jsp", username, "User logged off.");

if (invalidate) {
%> 
	<c:redirect url="/login?invalidate=true"/>
	<%
} 
%>
<c:redirect url="/enviosCentro"/>